package com.shangfutai.android.keymonitoringf5

import android.app.KeyguardManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log

class ScreenBoradcastReceiver : BroadcastReceiver() {


    override fun onReceive(context: Context, intent: Intent) {
        val action=intent.action
        Log.i("==================>",""+action)
        when(action){
            Intent.ACTION_SCREEN_ON->{
                val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
                keyguardManager?.let {
                    if (it.inKeyguardRestrictedInputMode()){
                        val run = KeyMonitoringService.isRun
                        if (!run){
                            val alarmIntent=Intent(context,KeyMonitoringService::class.java)
                            alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            context.startActivity(alarmIntent)
                        }


                    }
                }
            }
            Intent.ACTION_SCREEN_OFF->{
                val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
                keyguardManager?.let {
                    if (it.inKeyguardRestrictedInputMode()){

                        val alarmIntent=Intent(context,ScreenActivity::class.java)
                        alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        context.startActivity(alarmIntent)
                    }
                }
            }
            Intent.ACTION_USER_PRESENT->{
                val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
                keyguardManager?.let {
                    if (it.inKeyguardRestrictedInputMode()){
                        val run = KeyMonitoringService.isRun
                        if (!run){
                            val alarmIntent=Intent(context,KeyMonitoringService::class.java)
                            alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            context.startActivity(alarmIntent)
                        }

                    }
                }
            }
        }
    }
}